import 'package:enjoy_plus_hm/utils/eventbus.dart';
import 'package:enjoy_plus_hm/utils/toast.dart';
import 'package:flutter/material.dart';

import '../../utils/http.dart';

class ProfilePage extends StatefulWidget {
  ProfilePage({super.key, required this.userInfo});
  Map userInfo;

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  Map userInfo = {"id": "", "avatar": null, "nickName": null};
  final TextEditingController _nickNameController = TextEditingController();
  @override
  void initState() {
    super.initState();
    setState(() {
      userInfo = widget.userInfo;
      _nickNameController.text = userInfo['nickName'] ?? '';
    });
  }

  void editNickName() async {
    String nickName = _nickNameController.text;
    if (nickName == '') return ToastUtil.showError('昵称不能为空');
    if (nickName.length > 10) {
      return ToastUtil.showError('昵称不能超过10个字符');
    }
    try {
      await http.put('/userInfo', data: {"nickName": nickName});
      ToastUtil.showSuccess('修改昵称成功');
    } catch (e) {
      ToastUtil.showError('修改昵称失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('个人信息'),
        leading: IconButton(
          onPressed: () {
            eventBus.fire(profileToMineEvent(_nickNameController.text));
            Navigator.pop(context);
          },
          icon: Icon(Icons.arrow_back_ios),
        ),
      ),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.all(10),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('头像'),
                    Row(
                      children: [
                        userInfo['avatar'] != null
                            ? Image.network(
                              userInfo['avatar'],
                              width: 32,
                              height: 32,
                            )
                            : Image.asset(
                              'assets/images/avatar_1.jpg',
                              width: 32,
                              height: 32,
                            ),
                        const SizedBox(width: 10),
                        Text('>'),
                      ],
                    ),
                  ],
                ),
                const SizedBox(height: 30),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('昵称'),
                    Expanded(
                      child: TextField(
                        controller: _nickNameController,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: '请输入昵称',
                        ),
                        textAlign: TextAlign.right,
                        onEditingComplete: () {
                          editNickName();
                        },
                      ),
                    ),
                    const SizedBox(width: 10),
                    Text('>'),
                  ],
                ),
                // const SizedBox(height: 30),
                // ElevatedButton(
                //   onPressed: () {},
                //   child: Text('修改信息', style: TextStyle(color: Colors.white)),
                //   style: ElevatedButton.styleFrom(
                //     minimumSize: Size(double.infinity, 48),
                //     backgroundColor: Color.fromARGB(255, 133, 179, 199),
                //     shape: RoundedRectangleBorder(
                //       borderRadius: BorderRadius.circular(20),
                //     ),
                //   ),
                // ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
